#!/usr/bin/env python

import sys, os, time, thread
import glib, gobject
import pygst
pygst.require("0.10")
import gst

class Radio:
	
	def __init__(self):
		
	  # First create our pipeline
	  pipe = gst.Pipeline("mypipe")
		
	  # Create a software mixer with "Adder"
	  adder = gst.element_factory_make("adder","audiomixer")
    

	  #Create microphone
	  microphone = gst.element_factory_make("pulsesrc","microphone")
	  microphone.set_property("device","alsa_input.pci-0000_02_02.0.analog-stereo")
	  mic_volume = gst.element_factory_make("volume","mic_volume")
	  mic_volume.set_property("volume", 1)
	  mic_audioconvert = gst.element_factory_make("audioconvert","mic_audioconvert")

	  pipe.add(microphone, mic_volume, mic_audioconvert, adder)
    
	  #Link the microphone to the mixer
	  gst.element_link_many(microphone, mic_volume, mic_audioconvert, adder)
    
	  #Create the music 
	  source = gst.element_factory_make("filesrc", "file-source")
	  source.set_property("location", "/mnt/c_drive/1.mp3")
	  decoder = gst.element_factory_make("mad", "mp3-decoder")
	  songaudioconvert = gst.element_factory_make("audioconvert", "songaudioconvert")
	  volume_control = gst.element_factory_make("volume", "volume_control")
	  volume_control.set_property("volume", 0.1)
    
	  pipe.add(source, decoder, songaudioconvert, volume_control)
    
	  #Link the music to the mixer
	  gst.element_link_many(source, decoder, songaudioconvert, volume_control, adder)
    
    
    
	  #Create splitter which will split the output  - for stream and output
	  splitter = gst.element_factory_make("tee", "splitter")
	  pipe.add(splitter)
	  adder.link(splitter)
    
    
	  #Create output
	  output = gst.element_factory_make("pulsesink", "audio_out")
	  audioconvert = gst.element_factory_make("audioconvert","audioconvert")
    
	  directoutput = gst.element_factory_make("queue", "directoutput")
	  pipe.add(directoutput)
    
	  pipe.add(audioconvert, output)
    
	  #Link adder to the output
	  #gst.element_link_many(adder, audioconvert, output)
	  gst.element_link_many(splitter, directoutput, audioconvert, output)
    
	  #Create streamer
	  vor = gst.element_factory_make("vorbisenc", "vorbisenc")
	  oggmux = gst.element_factory_make("oggmux", "oggmux")
	  shout2send = gst.element_factory_make("shout2send", "shout2send")
    
	  shout2send.set_property("mount", "/stream.ogg")
	  shout2send.set_property("port", 8000)
	  shout2send.set_property("ip", "192.168.1.110")
	  shout2send.set_property("password", "hackme")
	  pipe.add(vor, oggmux, shout2send)
	  #gst.element_link_many(adder, vor, oggmux, shout2send)
	  gst.element_link_many(splitter, vor, oggmux, shout2send)

	  # Start the playback
	  pipe.set_state(gst.STATE_PLAYING)
	  
	  bus = source.get_bus()
	  print bus
	  bus.add_signal_watch()
	  bus.connect("message", self.on_message)

	def on_message(self, bus, message):
		print "message"
		t = message.type
		if t == gst.MESSAGE_EOS:
			self.player.set_state(gst.STATE_NULL)
			self.playmode = False
		elif t == gst.MESSAGE_ERROR:
			self.player.set_state(gst.STATE_NULL)
			err, debug = message.parse_error()
			print "Error: %s" % err, debug
			self.playmode = False

	def start(self):
	      print "Aaaa"

radioClass = Radio()
thread.start_new_thread(radioClass.start, ())
gobject.threads_init()
loop = glib.MainLoop()
loop.run()
